let bookcaseApp = function(){
    return {
      init:function(){
          this.getNoversInfo();
      },
        //打开、关闭加载动画面板
        panelLoading:function(flag){
            if('open'==flag){
                $("#panelLoading").removeClass("hidden");
            }else{
                $("#panelLoading").addClass("hidden");
            }
        },
        //删除小说
      novelDelete:function(obj){
          if(!confirm("确定要删除这本心爱的小说？")){
              return;
          }
          let _this = this;
          let novelID = $(obj).attr("novel-id");
          if(novelID==''||$.trim(novelID)==''){
              alert("数据异常，请刷新重试");
              return;
          }
          $.post( "/novel/novelDelete",{
              id:novelID
          },function(data){
              alert("删除成功");
              //刷新列表
              _this.getNoversInfo();
          });
      },
        //导入小说
    novelImportTest:function(){
        let novelPath = $("#novelPath").val();
        if(novelPath==''||$.trim(novelPath)==''){
            alert("请输入小说路径");
            return;
        }
        this.panelLoading('open');
        let _this = this;
        $.post( "/novel/novelImportTest",{
            url_local:novelPath
        },function(data){
            data = JSON.parse(data);
            if(data.status=="err"){
                alert(data.message);
            }else{
                console.log(data.data.chapterNum);
                console.log(data.data.chapters);
                alert("可以导入");
            }
            _this.panelLoading('close');
        });
      },
        //导入小说
      novelImport:function(){
        let novelPath = $("#novelPath").val();
        if(novelPath==''||$.trim(novelPath)==''){
            alert("请输入小说路径");
            return;
        }
        this.panelLoading('open');
        let _this = this;
          $.post( "/novel/novelInsert",{
              url_local:novelPath,
              author:$("#novelAuthor").val(),
              url_web:$("#novelUrlWeb").val(),
              cover:$("#novelCover").val()
          },function(data){
              data = JSON.parse(data);
              if(data.status=="err"){
                  alert(data.message);
              }else{
                  alert("导入成功");
                  //刷新列表
                  _this.getNoversInfo();
                  _this.novelImport0('close');
              }
              _this.panelLoading('close');
          });
      },
        //打开、关闭导入小说对话框
      novelImport0:function(flag){
          if('open'==flag){
              $("#novelImportDialog").removeClass("hidden");
          }else{
              $("#novelImportDialog").addClass("hidden");
          }
      },
        //获取小说列表
      getNoversInfo:function(){
          $.post("/novel/novelInfo",
              function(data){
                  data = JSON.parse(data);
                  let arr = data.data.data,str="",tmp,tmpSize;
                  for(let i=0;i<arr.length;i++){
                      tmp = arr[i];
                      //1048576 = 1024*1024
                      tmpSize = Math.floor(tmp.size/1048576*100)/100;
                      str += "<tr><td><a href='#' onclick='bookcaseApp.toUrl(this)'" +
                          " data-url-db-name='"+tmp.url_db_name+"'" +
                          " novel-id='"+tmp.id+"'>"+
                          tmp.name+"</a></td>" +
                          "<td>"+tmp.author+"</td>" +
                          "<td>"+tmpSize+" M</td>" +
                          "<td><a href='#'  onclick='bookcaseApp.novelDelete(this)' novel-id='"+tmp.id+"'>删除</a></td></tr>"
                  }
                  $("#novelList").html(str);
                  $("#novelNum").text(arr.length);
          });
      },
        toUrl:function(obj){
           let dbName = $(obj).attr("data-url-db-name");
           let novelID = $(obj).attr("novel-id");
           sessionStorage.setItem("url-db-name",dbName);
           sessionStorage.setItem("novel-id",novelID);
           location.href="novelChapter.html";
        }
    };
}();
$(document).ready(function(){
    bookcaseApp.init();
});